package com.zishi.venuesbooking.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zishi.venuesbooking.R;
import com.zishi.venuesbooking.entity.MyRecord;
import com.zishi.venuesbooking.entity.Reserve;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import de.hdodenhof.circleimageview.CircleImageView;

public class MyRecordAdapter extends RecyclerView.Adapter<MyRecordAdapter.MyRecordViewHolder> {

    private List<MyRecord> myRecords;

    public MyRecordAdapter(List<MyRecord> myRecords) {
        this.myRecords = myRecords;
    }

    @Override
    public MyRecordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_my_record, parent, false);
        return new MyRecordViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyRecordViewHolder holder, int position) {
        MyRecord myRecord = myRecords.get(position);
        holder.bind(myRecord);
    }

    @Override
    public int getItemCount() {
        return myRecords.size();
    }

    // 更新数据
    public void updateData(List<MyRecord> newMyRecords) {
        myRecords.clear();
        myRecords.addAll(newMyRecords);
        notifyDataSetChanged();
    }

    // 添加新的预约记录
    public void addMyRecord(MyRecord myRecord) {
        myRecords.add(0, myRecord); // 添加到列表顶部
        notifyItemInserted(0);
    }

    static class MyRecordViewHolder extends RecyclerView.ViewHolder {
        private final CircleImageView civAvatar = itemView.findViewById(R.id.civ_venue_avatar);
        private final TextView tvVenueName = itemView.findViewById(R.id.tv_venue_name);
        private final TextView tvRecordDate = itemView.findViewById(R.id.tv_record_date);

        public MyRecordViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void bind(MyRecord myRecord) {
            // 加载头像
            Glide.with(itemView.getContext())
                    .load(myRecord.getRecordVenueAvatar())
                    .placeholder(R.drawable.avata_1)
                    .into(civAvatar);

            tvVenueName.setText(myRecord.getRecordVenueName());
            tvRecordDate.setText(myRecord.getRecordDate());
        }

        private String formatTime(long time) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            return sdf.format(new Date(time));
        }
    }
}
